package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.monitor.entity.ErpOrderd;
import com.monitor.entity.ErpOrderdMateriel;
import com.monitor.service.ErpOrderdMaterielService;
import com.monitor.service.ErpOrderdService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 需求汇总物料表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-11
 */
@RestController
@RequestMapping("/erpOrderdMateriel")
@Api(description="需求汇总物料")
public class ErpOrderdMaterielController {


    @Resource
    private ErpOrderdService orderdService;
    @Resource
    private ErpOrderdMaterielService orderdMaterielService;


    @RequestMapping("/inertOrUpdateOrderdMateriel")
    @ApiOperation("添加Or修改需求")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpOrderdMateriel> inertOrUpdateOrderdMateriel(ErpOrderdMateriel orderdMateriels) {
        Result<ErpOrderdMateriel>  result=new Result<ErpOrderdMateriel>();
        try{

            orderdMaterielService.insertOrUpdate(orderdMateriels);
            result.setCount(1);
            result.setData(orderdMateriels);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getOrderdMateriel")
    @ApiOperation("根据客户id获取需求详情")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpOrderdMateriel>> getOrderdMateriel(Integer custId) {
        Result<List<ErpOrderdMateriel>>  result=new Result<List<ErpOrderdMateriel>>();
        try{
            List<ErpOrderd > orderd=orderdService.selectList(new EntityWrapper<ErpOrderd>().eq("customer_id",custId));
            List<ErpOrderdMateriel> orderdMateriels=new ArrayList<ErpOrderdMateriel>();
            for (ErpOrderd  o:orderd) {
                ErpOrderdMateriel orderdMateriel=orderdMaterielService.selectOne(new EntityWrapper<ErpOrderdMateriel>().eq("order_id",o.getId()));
                orderdMateriels.add(orderdMateriel);
            }
            result.setCount(orderdMateriels.size());
            result.setData(orderdMateriels);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

















}

